class Solution 
{
public:
    vector<vector<int>> threeSum(vector<int>& nums) 
    {
        vector<vector<int>> ret;
        if(nums.size() < 3)
            return ret;
        for(int i = 0; i < nums.size() - 2; i++)
        {
            int target = -nums[i];
            int left = i + 1, right = nums.size() - 1;
            while(left < right)
            {
                if(nums[left] + nums[right] == target)
                {
                    ret.push_back({nums[left], nums[right], nums[i]});
                    right--;
                    while(nums[right+1] == nums[right])
                        right--;
                    left++;
                    while(nums[left-1] == nums[left])
                        left++;
                }
                else if(nums[left] + nums[right] > target)
                {
                    right--;
                    while(nums[right+1] == nums[right])
                        right--;
                }    
                else
                {
                    left++;
                    while(nums[left-1] == nums[left])
                        left++;
                }
            }
        }
        return ret;
    }
};


class Solution
{
public:
    int trap(vector<int>& height)
    {
        int left = 0, right = height.size() - 1;
        int maxleft = 0, maxright = 0;
        int area = 0;
        while(left < right)
        {
            maxleft = max(maxleft, height[left]);
            maxright = max(maxright, height[right]);
            if(maxleft < maxright)
            {
                area += maxleft - height[left];
                left++;
            }
            else
            {
                area += maxright - height[right];
                right--;
            }
        }
        return area;
    }
};
